﻿using DataVirtualization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;
using System.Windows.Media;
using WinDataInspector.Model;

namespace WinDataInspector.Behavior
{
    class HexCellBehavior : Behavior<TextBox>
    {
        int column;
        int row;

        public int RowIndex
        {
            get { return (int)GetValue(RowIndexProperty); }
            set { SetValue(RowIndexProperty, value); }
        }

        public static readonly DependencyProperty RowIndexProperty =
            DependencyProperty.Register("RowIndex", typeof(int), typeof(HexCellBehavior));


        private T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            // get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            // we’ve reached the end of the tree
            if (parentObject == null) return null;

            // check if the parent matches the type we’re looking for
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // use recursion to proceed with next level
                return FindVisualParent<T>(parentObject);
            }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            TextBox textbox = this.AssociatedObject;

            textbox.GotFocus += Textbox_GotFocus;

            //get parent item
            StackPanel panel = FindVisualParent<StackPanel>(this.AssociatedObject);

            ContentPresenter content = FindVisualParent<ContentPresenter>(this.AssociatedObject);

            int index = panel.Children.IndexOf(content as UIElement);



            //HexItem item2 = FindVisualParent<HexItem>(this.AssociatedObject);

            //int rows = theGrid.Rows;
            //int columns = theGrid.Columns;

            //int index = theGrid.Children.IndexOf(childElement);

            //int row = index / columns;  // divide
            //int column = index % columns;  // modulus            
        }

        private void Textbox_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
